package com.northwest_man.refactor_tool;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReaderForExcel {
    private static final Map<String, String> langName = new HashMap<String, String>() {
        {
            put("中文", "zh");
            put("English", "en");
            put("French", "fr");
            put("Arabic", "ar");
            put("Portuguese", "pt");
            put("Bengali", "bn");
        }
    };
    public static Map<String, Map<String, String>> read() {
//        String fileName = "D:\\Projects\\AndroidRefactorTools\\output.xlsx"; // Excel文件名
        String fileName = "D:\\Users\\Downloads\\oraimo APP字段翻译.xlsx"; // Excel文件名

        try (FileInputStream fileInputStream = new FileInputStream(fileName);
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个工作表中

            Map<String, Map<String, String>> dataMap = new HashMap<>();

            Row headerRow = sheet.getRow(0); // 头部行，包含语言列表
            int lastCellNum = headerRow.getLastCellNum(); // 列数

            // 遍历每一列（从第二列开始），获取语言
            for (int colIndex = 1; colIndex < lastCellNum; colIndex++) {
                Cell languageCell = headerRow.getCell(colIndex);
                String language = languageCell.getStringCellValue().trim();
                if (language == null || language.trim().isEmpty()) continue;
                final String langNameValue;
                if ((langNameValue = langName.get(language)) != null) {
                    language = langNameValue;
                }
                Map<String, String> languageData = new HashMap<>();

                // 遍历每一行（从第二行开始），获取键值对
                int lastRowIndex = sheet.getLastRowNum();
                for (int rowIndex = 1; rowIndex <= lastRowIndex; rowIndex++) {
                    Row dataRow = sheet.getRow(rowIndex);

                    Cell keyCell = dataRow.getCell(0);
                    Cell valueCell = dataRow.getCell(colIndex);

                    String key = keyCell.getStringCellValue().trim();
                    key = getKey(key);

                    StringBuilder value = new StringBuilder((valueCell != null) ? valueCell.getStringCellValue().trim() : "");
                    final Pattern pattern = Pattern.compile("[^\\\\](')");
                    final ArrayList<Point> pointList = new ArrayList<>();
                    final Matcher matcher = pattern.matcher(value);
                    while (matcher.find()) {

                        final int start = matcher.start(1);
                        final int end = matcher.end(1);
                        pointList.add(new Point(start, end));
                    }
                    Collections.reverse(pointList);
                    for (final Point point : pointList) {
                        value.replace(point.x, point.y, "\\'");
                    }
                    languageData.put(key, value.toString());
                }

                dataMap.put(language, languageData);
            }
            return dataMap;
            // 输出读取的数据
          /*  for (String language : dataMap.keySet()) {
                System.out.println("Language: " + language);
                Map<String, String> languageData = dataMap.get(language);
                for (String key : languageData.keySet()) {
                    String value = languageData.get(key);
                    System.out.println("Key: " + key + ", Value: " + value);
                }
                System.out.println();
            }*/

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getKey(final String key) {
        if (key == null) return key;
        final Pattern pattern = Pattern.compile("name[^ \"]+?\"([^\"]+?)\"");
        final Matcher matcher = pattern.matcher(key);
        if (matcher.find()) {
            return matcher.group(1);
        } else return key;
    }
}